package com.test.JVM;

import java.lang.ref.SoftReference;

public class SoftRefDemo {

    /**
     *
     * 内存够用的时候就保留，不够就回收
     */
    public static void softRef_Enough(){
        Object o1 = new Object();
        SoftReference softReference = new SoftReference(o1);
        System.out.println(o1);
        System.out.println(softReference.get());

        o1 = null;
        System.gc();

        System.out.println(o1);
        System.out.println(softReference.get());
    }
    public static void softRef_NotEnough(){
        Object o1 = new Object();
        SoftReference softReference = new SoftReference(o1);
        System.out.println(o1);
        System.out.println(softReference.get());

        o1 = null;
        //System.gc();

        try {
            Byte[] bytes = new Byte[30 * 1024 * 1024];
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.out.println(o1);
            System.out.println(softReference.get());
        }


    }

    public static void main(String[] args) {
        softRef_Enough();
    }
}
